<template>
  <section class="main">
      <input id="toggle-all" class="toggle-all" type="checkbox" v-model="ischekall"/>
      <label for="toggle-all" >Mark all as complete</label>
      <ul class="todo-list">
        <!-- 当任务已完成，可以给 li 加上 completed 类，会让元素加上删除线 -->
        <li :class="{ completed:item.isDone }" v-for="item in isshow" :key="item.id">
          <div class="view">
            <input class="toggle" type="checkbox" :checked="item.isDone" @change="ischange(item.id)"/>
            <label>{{ item.name }}</label>
            <button class="destroy" @click="del(item.id)"></button>
          </div>
        </li>
    
      </ul>
    </section>
</template>

<script>
export default {
  props: ['list','type'],
  methods: {
    ischange(id) {
      this.$emit('ischange',id)
    },
    del(id) {
      this.$emit('del',id)
    }
  },
  computed: {
    isshow() {
      if (this.type === 'active') {
       return this.list.filter(x => {
          return x.isDone
        })
      } else if (this.type === 'completed') {
       return this.list.filter(x => {
          return !x.isDone
        })
      }
      return this.list
    },
    ischekall: {
      get() {
        return this.list.every(x => {
          x.isDone
        })
      },
      set(val) {
        this.$emit('ischekall',val)
      }
    }
  }
}
</script>

<style>

</style>